﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BullsEyeGame
{
    /// <summary>
    /// A form containing a variety of colors to choose from
    /// </summary>
    internal class FormColorBoard : Form
    {
        private const bool k_Visible = true;
        private const string k_TitleOfForm = "Pick a color:";
        private const int k_SizeOfColorButtonEdge = 45;
        private const int k_SizeOfColorButtonPadding = 4;
        private List<Button> m_colorsButtons = new List<Button>();
        private Color m_LastChosenColor;

        /// <summary>
        /// A constructor for the FormColorBoard
        /// </summary>
        public FormColorBoard()
        {
            InitializeComponent();
        }
        
        /// <summary>
        /// Property to get the last chosen color of this board
        /// </summary>
        public Color LastChosenColor
        {
            get { return m_LastChosenColor; }
        }

        private void InitializeComponent()
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.AutoSize = true;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.Text = k_TitleOfForm;
            this.Visible = !k_Visible;
            addColorButtons();
        }

        private void addColorButtons()
        {
            List<KnownColor> allGuessColors = getAllGuessColors();
            List<KnownColor> firstRowColors, secondRowColors;
            int numberOfColorsInFirstRow, numberOfColorsInSecondRow, firstRowTop, secondRowTop;
             
            firstRowTop = 0;
            numberOfColorsInFirstRow = Guess.NumberOfGuessLetters / 2;
            firstRowColors = allGuessColors.GetRange(0, numberOfColorsInFirstRow);
            addColorButtonsInRow(numberOfColorsInFirstRow, firstRowTop, firstRowColors);

            secondRowTop = k_SizeOfColorButtonPadding + k_SizeOfColorButtonEdge;
            numberOfColorsInSecondRow = Guess.NumberOfGuessLetters - numberOfColorsInFirstRow;
            secondRowColors = allGuessColors.GetRange(numberOfColorsInFirstRow, numberOfColorsInSecondRow);
            addColorButtonsInRow(numberOfColorsInSecondRow, secondRowTop, secondRowColors);
        }

        private List<KnownColor> getAllGuessColors()
        {
            List<KnownColor> allGuessColors = new List<KnownColor>();
            string[] allLettersNames = Enum.GetNames(typeof(Guess.eGuessLetters));

            foreach (string letterName in allLettersNames)
            {
                KnownColor color = LettersColorsTranlator.Instance.TranslateLetterNameToColor(letterName);
                allGuessColors.Add(color);
            }

            return allGuessColors;
        }

        private void addColorButtonsInRow(int i_NumberOfColorsInRow, int i_Top, List<KnownColor> i_ColorsToUse)
        {
            Button currentColorBoardSubUnitButton;

            for (int i = 0; i < i_NumberOfColorsInRow; ++i)
            {
                currentColorBoardSubUnitButton = createColorBoardButton(i, i_Top, i_ColorsToUse);
                m_colorsButtons.Add(currentColorBoardSubUnitButton);
                this.Controls.Add(currentColorBoardSubUnitButton);
            }
        }

        private Button createColorBoardButton(int i_ButtonColumnIndex, int i_Top, List<KnownColor> i_ColorsToUse)
        {
            int left, top;
            Button buttonColorBoardSubUnit;

            buttonColorBoardSubUnit = new Button();
            buttonColorBoardSubUnit.Size = new Size(k_SizeOfColorButtonEdge, k_SizeOfColorButtonEdge);
            buttonColorBoardSubUnit.BackColor = Color.FromKnownColor(i_ColorsToUse[i_ButtonColumnIndex]);
            left = (i_ButtonColumnIndex * (k_SizeOfColorButtonPadding + k_SizeOfColorButtonEdge)) + k_SizeOfColorButtonPadding;
            top = i_Top + k_SizeOfColorButtonPadding;
            buttonColorBoardSubUnit.Location = new Point(left, top);
            buttonColorBoardSubUnit.Click += buttonColorBoardSubUnit_Click;

            return buttonColorBoardSubUnit;
        }

        private void buttonColorBoardSubUnit_Click(object sender, EventArgs e)
        {
            Button buttonColorBoardSubUnitClicked = (Button)sender;
            
            m_LastChosenColor = buttonColorBoardSubUnitClicked.BackColor;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }        
    }
}
